#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>


// example 将一个字符串 hello ,linux io 写入到文件中， 再读取出来

int main(int argc, char const *argv[])
{
    int fd;
    char buffer[64] = "hello, linux io";
    char rbuffer[64] = {0};
<<<<<<< Updated upstream:pashe4/01file_io/02read_write/demo3.c
    ssize_t wbytes = 0, rbytes = 0;
=======
    int wbytes = 0, rbytes = 0;
>>>>>>> Stashed changes:pashe4/02read_write/demo3.c

    if (argc != 2) {
        printf("usage: %s <filename>\n", argv[0]);
        return -1;
    }
    fd = open(argv[1], O_RDWR | O_CREAT);
    if (fd == -1) {
        perror("open error \n");
        return -1;
    }

<<<<<<< Updated upstream:pashe4/01file_io/02read_write/demo3.c
    wbytes = write(fd, buffer , strlen(buffer));
=======
    wbytes = write(fd, buffer , 10);
>>>>>>> Stashed changes:pashe4/02read_write/demo3.c

    if (wbytes == -1) {
        perror("write error \n");
    }
    printf("write bytes: %d\n", wbytes);
    
    //重新定位读写文件偏移量 到 文件开头
    lseek(fd, 0, SEEK_SET);

<<<<<<< Updated upstream:pashe4/01file_io/02read_write/demo3.c
    // read 读取字节数到rbuffer中
    rbytes = read(fd, rbuffer, wbytes);
=======
    // read
    rbytes = read(fd, rbuffer, 10);
>>>>>>> Stashed changes:pashe4/02read_write/demo3.c
    if (rbytes == -1) {
        perror("read error \n");
        return -1;
    }
    printf("read rbuffer : %s \n", rbuffer);

    close(fd);
    return 0;
}
